<%@page import="javax.jdo.Query"%>
<%@page import="org.passout.projecter.model.Project"%>
<%@page import="org.passout.projecter.model.PMF"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<html>
  <body>
   <form action="/createproject" method="post">
      <div><input type="text" name="pname"/>
      <input type="submit" value="AddProject" /></div>
    </form>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    PersistenceManager pm = PMF.get().getPersistenceManager();
	Query q = pm.newQuery(Project.class, "author == user");
	q.declareParameters("com.google.appengine.api.users.User user");
    List<Project> pojects = (List<Project>) q.execute(user);
    if (pojects.isEmpty()) {%>
		<p>No Projects.</p>
<%   } else {
    	if (pojects.size() == 1){
    		response.sendRedirect("/main.jsp?pname="+pojects.get(0).getName());
    	}
        for (Project g : pojects) {%>
			<div><a href="/main.jsp?pname=<%=g.getName() %>"> <%=g.getName() %> </a></div>
<%    	}
    }
    pm.close();
%>
  </body>
</html>